package com.nidu.demo.tenant.executor.command;

import com.alibaba.cola.dto.SingleResponse;
import com.nidu.demo.tenant.dto.TenantPackageAddCmd;
import com.nidu.demo.tenant.ability.TenantPackageDomainService;
import com.nidu.demo.tenant.convertor.TenantPackageConvertor;
import com.nidu.demo.tenant.dto.TenantPackageCO;
import com.nidu.demo.tenant.gateway.TenantPackageGateway;
import com.nidu.demo.tenant.model.TenantPackage;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 租户套餐创建命令执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class TenantPackageAddCmdExe {

    private final TenantPackageDomainService domainService;

    private final TenantPackageGateway gateway;

    private final TenantPackageConvertor convertor;

    public SingleResponse<TenantPackageCO> execute(TenantPackageAddCmd cmd) {
        // 1. 转换为领域对象
        TenantPackage tenantPackage = TenantPackage.create(cmd.getName(), cmd.getMenuIds(), cmd.getSortOrder());

        // 2. 校验对象
        domainService.validateDuplicate(tenantPackage.getName(), tenantPackage.getId());

        // 3. 调用网关保存
        TenantPackage save = gateway.create(tenantPackage);

        // 4. 转换为DTO返回
        TenantPackageCO tenantPackageCO = convertor.toClientObject(save);

        return SingleResponse.of(tenantPackageCO);
    }
}
